import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cookie: '',
    userInfo: function (){
      var item = sessionStorage.getItem("userInfo");
      if(item){
        return JSON.parse(item)
      }else return {}
    }
  },
  mutations: {
    //setters
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
      sessionStorage.setItem("userInfo", JSON.stringify(userInfo)); //将对象序列化，因为sessionStorage无法存储对象
    },
    SET_COOKIE: (state, cookie) => {
      state.userInfo = cookie;
      localStorage.setItem("cookie", cookie); //这个会比sessionStorage存在的久
    },
    REMOVE_INFO: (state) => {
      state.cookie = null;
      state.userInfo = {};
      sessionStorage.setItem("userInfo", {});
      localStorage.setItem("cookie", '');
    }
  },
  getters: {
    getUserInfo:(state)=>{
      return state.userInfo;
    }
  },
  actions: {},
  modules: {}
})